package com.tools.cleanmaster.taskmain;

import android.content.Context;
import android.content.res.Resources;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ConfigUtils {
    public static final String TAG = "ConfigUtils";

    public static String[] loadArrayFromFile(String fileName) {
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(TAG, "start to load file: " + fileName);
        }
        List<String> list = new ArrayList<String>();
        InputStream inputStream = null;
        Context ctx = CleanApplication.getInstance();
        try {
            Resources resources = ctx.getResources();
            if (resources == null) {
                return new String[0];
            }

             inputStream = resources.getAssets().open(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                list.add(tempString);
            }
        } catch (IOException e) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(TAG, "fail to load file: " + fileName + " for " + e);
            }
        } catch (Exception e) {
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    if (FeatureConfig.DEBUG_LOG) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (!list.isEmpty()) {
            return list.toArray(new String[0]);
        } else {
            return new String[0];
        }
    }
}
